package com.ddzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.cst.SysConst;
import com.ddzh.dto.OrderDto;
import com.ddzh.mapper.OrderMapper;
import com.ddzh.pojo.Member;
import com.ddzh.pojo.Order;
import com.ddzh.pojo.OrderSetting;
import com.ddzh.res.OrderRes;
import com.ddzh.service.MemberService;
import com.ddzh.service.OrderService;
import com.ddzh.service.OrderSettingService;
import com.ddzh.utils.DateUtil;
import com.ddzh.utils.RedisUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

//    @Resource
//    private RedisTemplate redisTemplate;

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    @Reference
    private OrderSettingService orderSettingService;

    @Transactional
    @Override
    public Long appoOrder(OrderDto orderDto) {
        //TODO：前面的验证部分单独抽出来，作为方法，该方法中留操作的核心内容
       //0.验证码校验
        //从redis中取出验证码
//        String code = (String)redisTemplate.opsForValue().get(SysConst.SMS_VALIFYCODE + orderDto.getTelephone());
        String code = (String) RedisUtil.get(SysConst.SMS_VALIFYCODE + orderDto.getTelephone());
        //判null
        if (code==null){
            throw new RuntimeException("验证码已失效或没有发送验证码");
        }
        //获取前端传过来的验证码
        String frontCode = orderDto.getValidateCode();
        if (code.equals(frontCode)==false){
            throw new RuntimeException("校验码匹配错误");
        }
        //1.判断是否重复预约
        //根据手机号查询VIP用户
        Member member = memberService.getByTele(orderDto.getTelephone());
        //判null
        String orderDate = DateUtil.formatDate(orderDto.getOrderDate());
        if (member !=null){
            //用户已经存在
            //判断用于重复预约:根据 用户id 和 体检日期 从order表中判定唯一预约状态
            Order order = getByMemberIdAndOrderDate(member.getId(), orderDate);
            if (order!=null){
                throw new RuntimeException("已成功预约，不能重复预约");
            }
        }
        //2.预约当天是否可以预约校验
        //根据体检日期查询预约设置表
        OrderSetting orderSetting = orderSettingService.findByDate(orderDate);
        if (orderSetting.getOrderNumber()==orderSetting.getOrdered()){
            throw new RuntimeException("当天预约人数已达上限，请重新选择");
        }
        //上面是校验信息

        //模拟服务器火爆,网络延迟


//        try {
//            Thread.sleep(10000);
//            return null;
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }



        //3.预约设置预约数修改
        orderSetting.setOrdered(orderSetting.getOrdered()+1);
       //4.会员信息维护
        if (member==null){
            //如果用户不存在，创建新用户
            member = new Member();
            member.setCreateDate(new Date());
        }
        member.setName(orderDto.getName());
        member.setSex(orderDto.getSex());
        member.setTelephone(orderDto.getTelephone());
        memberService.saveOrUpdate(member);
       //5.新增预约订单
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDto.getOrderDate());
        order.setSetmealId(orderDto.getSetmealId());
        orderService.save(order);
        //封装响应给前端的数据 - 订单id
        return order.getId();
    }

    @Override
    public Order getByMemberIdAndOrderDate(Long memberId, String orderDate) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public OrderRes getOrderRes(Long orderId) {
        //查询数据库，封装响应信息
        return this.baseMapper.getOrderRes(orderId);
    }
}
